package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.Scanner;

public enum Menu {

    LISTBOOKS("List Books", "1") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream,
                                 BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println(bookCollection);
            return new ViewState(currentUser, true);
        }
    },
    CHECKOUTBOOK("Checkout Book", "2") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream,
                                 BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {

            currentUser = login(scanner, printStream, userCollection, currentUser);
            if (currentUser != null) {
                printStream.print("Name of book: ");
                String bookToBeProcessed = scanner.next();

                if (bookCollection.checkOut(bookToBeProcessed)) {
                    printStream.println("\n" + "Thank You! Enjoy the Book" + "\n");
                } else {
                    printStream.println("\n" + "Sorry! Book not in store" + "\n");
                }
                printStream.println(bookCollection);
            }
            return new ViewState(currentUser, true);
        }
    },
    CHECKINBOOK("Return Book", "3") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream,
                                 BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {

            currentUser = login(scanner, printStream, userCollection, currentUser);
            if (currentUser != null) {
                printStream.print("Name of book: ");
                String bookToBeProcessed = scanner.next();
                if (bookCollection.checkIn(bookToBeProcessed)) {
                    printStream.println("\n" + "Thank you for returning the book" + "\n");
                } else {
                    printStream.println("\n" + "That is not a valid book to return" + "\n");
                }
                printStream.println(bookCollection);
            }
            return new ViewState(currentUser, true);
        }
    },
    LISTMOVIES("List Movies", "4") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, BookCollection bookCollection,
                                 MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println(movieCollection);
            return new ViewState(currentUser, true);

        }
    },
    CHECKOUTMOVIE("Checkout Movie", "5") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, BookCollection bookCollection,
                                 MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.print("Name of movie: ");
            String movieToBeProcessed = scanner.next();

            if (movieCollection.checkOut(movieToBeProcessed)) {
                printStream.println("\n" + "Thank You! Enjoy the movie" + "\n");
            } else {
                printStream.println("\n" + "Sorry! movie not in store" + "\n");
            }
            printStream.println(movieCollection);
            return new ViewState(currentUser, true);
        }
    },
    CHECKINMOVIE("Checkin Movie", "6") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream,
                                 BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.print("Name of movie: ");
            String movieToBeProcessed = scanner.next();
            if (movieCollection.checkIn(movieToBeProcessed)) {
                printStream.println("\n" + "Thank you for returning the movie" + "\n");
            } else {
                printStream.println("\n" + "That is not a valid movie to return" + "\n");
            }
            printStream.println(movieCollection);
            return new ViewState(currentUser, true);
        }
    },
    MYINFO("My Info", "7") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, BookCollection bookCollection,
                                 MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            currentUser = login(scanner, printStream, userCollection, currentUser);
            if (currentUser != null) {
                printStream.println(currentUser);
            }
            return new ViewState(currentUser, true);
        }
    },
    QUIT("Quit", "8") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream,
                                 BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            return new ViewState(currentUser, false);
        }
    },
    INVAlID("Invalid", "") {
        @Override
        public ViewState execute(Scanner scanner, PrintStream printStream, BookCollection bookCollection, MovieCollection movieCollection, UserCollection userCollection, User currentUser) {
            printStream.println("Invalid option\n");
            return new ViewState(currentUser, true);
        }

        @Override
        public String toString() {
            return "";
        }
    };
    private final String name;
    private final String id;

    Menu(String name, String id) {
        this.name = name;
        this.id = id;
    }

    private static User login(Scanner scanner, PrintStream printStream, UserCollection userCollection, User currentUser) {
        if (currentUser != null) {
            return currentUser;
        }
        printStream.println("You're not logged in");
        printStream.println("Username: ");
        String username = scanner.next();
        printStream.println("Password: ");
        String password = scanner.next();
        User lookedUpUser = userCollection.lookup(username, password);
        if (lookedUpUser == null) {
            printStream.println("Invalid login\n");
        }
        return lookedUpUser;
    }

    public abstract ViewState execute(Scanner scanner, PrintStream printStream,
                                      BookCollection bookCollection, MovieCollection movieCollection,
                                      UserCollection userCollection, User currentUser);

    public String toString() {
        return String.format("%s. %s%n", id, name);
    }

    public boolean matches(String input) {
        return id.equals(input);
    }
}
